﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Media;

namespace AirWar
{
    //枚举游戏状态
    public enum GameState
    {
        Open = 1,Close =2,Pause = 3
    }
    //枚举移动方向
    public enum Direction
    {
        Up = 1,Down = 2,Left = 3,Right = 4
    }
    //枚举敌方和我方
    public enum Side
    {
        Me = 1,Enemy = 2
    }
    public class Fighter
    {   //私有字段
        #region
        //飞机的所属方
        private Side _side;
        //飞机的坐标位置
        private Point _position;
        //飞机的移动方向
        private Direction _direction;
        //飞机的步长
        private int _step;
        //飞机的尺寸大小
        private Size _size;
        //飞机的生命值
        private int _lifeValue;
        //飞机的得分
        private int _score;
        //飞机当前显示的图片
        private Bitmap _curFighterBmp;
        //飞机的候选图片数组
        private Bitmap[] _fighterBmp = new Bitmap[4];
        //飞机的图片轮换标志
        private bool _bmpChangeFlag = true;
        //飞机的爆炸图片数组
        private Bitmap[] _explodeBitmap = new Bitmap[6];
        #endregion
        //公有属性
        #region
        //飞机的所属方
        public Side Side { get => _side; set => _side = value; }
        //飞机的坐标位置
        public Point Position { get => _position; set => _position = value; }
        //飞机的移动方向
        public Direction Direction { get => _direction; set => _direction = value; }
        //飞机的步长
        public int Step { get => _step; set => _step = value; }
        //飞机的尺寸大小
        public Size Size { get => _size; set => _size = value; }
        //飞机的生命值
        public int LifeValue { get => _lifeValue; set => _lifeValue = value; }
        //飞机的得分
        public int Score { get => _score; set => _score = value; }
        #endregion
        //类构造方法
        public Fighter(Side side, Size mapSize)
        {
            //设置飞机的所属方
            _side = side;
            //如果是我方飞机
            if (side == Side.Me)
            {
                //装载我方飞机的四张候选图片
                _fighterBmp[0] = new Bitmap("Image\\myFighterUpDown.png");
                _fighterBmp[1] = new Bitmap("Image\\myFighterUpDownLongFlame.png");
                _fighterBmp[2] = new Bitmap("Image\\myFighterLeft.png");
                _fighterBmp[3] = new Bitmap("Image\\myFighterRight.png");
                //设置我方飞机的透明色
                for (int i = 0; i <= 3; i++)
                    _fighterBmp[i].MakeTransparent(Color.Black);
                //设置我方飞机当前显示照片
                _curFighterBmp = _fighterBmp[0];
                //设置我方飞机尺寸大小
                _size = new Size(_curFighterBmp.Width, _curFighterBmp.Height);
                //设置初始移动方向向上
                _direction = Direction.Up;
                //设置我方飞机的初始位置为地图下方的正中央
                _position.X = mapSize.Width / 2 - _size.Width / 2;
                _position.Y = mapSize.Height - _size.Height - 25;
                //默认步长，生命值，得分
                _step = 15;
                _lifeValue = 100;
                _score = 0;
            }
            else if (side == Side.Enemy)
            {
                //装载所需的三张敌方飞机照片
                _fighterBmp[0] = new Bitmap("Image\\enemyFighterGreen.png");
                _fighterBmp[1] = new Bitmap("Image\\enemyFighterRed.png");
                _fighterBmp[2] = new Bitmap("Image\\enemyFighterYellow.png");
                //设置敌方飞机候选图片的透明色
                for (int i = 0; i <= 2; i++)
                    _fighterBmp[i].MakeTransparent(Color.Black);
                //随机选一张图片作为敌方飞机照片
                Random rand = new Random(DateTime.Now.Second);
                _curFighterBmp = _fighterBmp[rand.Next(0, 3)];
                //设置敌方飞机尺寸
                _size = new Size(_curFighterBmp.Width, _curFighterBmp.Height);
                //设置初始方向向下
                _direction = Direction.Down;
                //设置敌方飞机初始位置为地图上方的随机位置
                _position.X = rand.Next(0, mapSize.Width);
                _position.Y = _size.Height - _size.Height;
                //默认步长6，生命值2，默认得分0
                _step = 6;
                _lifeValue = 2;
                _score = 0;
                //装载六张飞机爆炸图片
                for(int i=0;i<=5;i++)
                {
                    _explodeBitmap[i] = new Bitmap("Image\\explode" + i.ToString() + ".png");
                    _explodeBitmap[i].MakeTransparent(Color.Black);
                }
            }
        }
        //(方法)飞机绘制
        public void DrawMe(Graphics g)
        {
            //绘制飞机图片
            g.DrawImage(_curFighterBmp, _position);
        }
        //(方法)飞机移动
        public void Move(Direction direction, Size mapSize)
        {
            //保存移动方向
            _direction = direction;
            //如果是我方飞机
            if (_side == Side.Me)
            {
                //如果向上
                if (_direction == Direction.Up)
                {
                    //设置飞机移动后所在位置
                    if (_position.Y >= _step)   //确保飞机不飞出地图
                        _position.Y = _position.Y - _step;  //否则对齐地图最上边
                    else
                        _position.Y = 0;
                    //设置飞机当前显示的图片
                    //为了前进效果更逼真，在0和1图片轮换
                    if (_bmpChangeFlag == true)
                        _curFighterBmp = _fighterBmp[0];
                    else
                        _curFighterBmp = _fighterBmp[1];
                    //切换轮换标志
                    _bmpChangeFlag = !_bmpChangeFlag;
                }
                //如果向下
                else if (_direction == Direction.Down)
                {
                    //设置飞机移动后所在位置
                    if (_position.Y <= mapSize.Height - _size.Height - _step)
                        _position.Y = _position.Y + _step;
                    else
                        _position.Y = mapSize.Height - _size.Height;
                    //设置飞机当前显示的图片
                    _curFighterBmp = _fighterBmp[0];
                }
                //如果向左
                else if (_direction == Direction.Left)
                {
                    if (_position.X >= _step)
                        _position.X = _position.X - _step;
                    else
                        _position.X = 0;
                    //设置飞机当前显示的图片
                    _curFighterBmp = _fighterBmp[2];
                }
                //如果向右
                else if (_direction == Direction.Right)
                {
                    if (_position.X <= mapSize.Width - _size.Width - _step)
                        _position.X = _position.X + _step;
                    else
                        _position.X = mapSize.Width - _size.Width;
                    //设置飞机当前显示的图片
                    _curFighterBmp = _fighterBmp[3];
                }
                //重新设置飞机的尺寸大小
                _size = new Size(_curFighterBmp.Width, _curFighterBmp.Height);
            }
            //如果是敌方飞机
            else
            {
                //如果向下移动
                if (_direction == Direction.Down)
                    _position.Y = _position.Y + _step;
                //如果向左
                else if (_direction == Direction.Left)
                {
                    if (_position.X > _step)
                    {
                        _position.X = _position.X - _step;
                        _position.Y = _position.Y + _step;
                    }
                    else
                    {
                        _position.X = 0;
                        _position.Y = _position.Y + _step;
                    }
                }
                //如果向右
                else if (_direction == Direction.Right)
                {
                    if (_position.X < mapSize.Width - _size.Width - _step)
                    {
                        _position.X = _position.X - _step;
                        _position.Y = _position.Y + _step;
                    }
                    else
                    {
                        _position.X = mapSize.Width + _size.Width;
                        _position.Y = _position.Y + _step;
                    }
                }
            }
        }
        //(方法)飞机开火
        public Bullet Fire()
        {
            //创建一个子弹类对象
            Bullet bullet = new Bullet(_side);
            //发射时子弹的位置等于飞机的所在位置
            //如果是我方子弹
            if (_side == Side.Me)
            {
                //根据发射方向的不同,设置我方子弹的初始位置
                if (_direction == Direction.Up || _direction == Direction.Down)
                    bullet._Position = new Point(Position.X + _size.Width / 2 - 4, Position.Y - _size.Height / 2);
                else if (_direction == Direction.Left)
                    bullet._Position = new Point(Position.X + _size.Width / 2 - 6, Position.Y - _size.Height / 2);
                else if (_direction == Direction.Right)
                    bullet._Position = new Point(Position.X + _size.Width / 2 - 8, Position.Y - _size.Height / 2);
            }
            //如果是敌方子弹
            else
                //设置敌方子弹的初始位置
                bullet._Position = new Point(Position.X + _size.Width / 2 - 4, Position.Y + _size.Height / 2 + 5);
            //返回子弹对象
            return bullet;
        }
        //(方法)飞机爆炸
        public void Explode(Graphics g)
        {
            for (int i = 0; i < 6; i++)
                g.DrawImage(_explodeBitmap[i], _position.X, _position.Y);
        }
        //(方法)播放音效
        public void PlaySound(String soundFile)
        {
            //播放音效
            SoundPlayer soundPlay = new SoundPlayer(soundFile);
            soundPlay.Play();
        }
    }
}
